/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AlcatrazRegistryGui.java
 *
 * Created on 05.03.2011, 19:55:41
 */

package alcatraz.client;

import java.util.logging.Level;
import alcatraz.common.IAlcatraz;
import alcatraz.common.AlcatrazLogger;

/**
 *
 * @author andreas
 */
public class AlcatrazRegistryGui extends javax.swing.JFrame implements IAlcatraz {

    /** reference to logger object */
    private static AlcatrazLogger logger = null;

    /** current index of combo box for set server address */
    private int cbServerAddrIdx = 0;

    /** Creates new form AlcatrazRegistryGui */
    public AlcatrazRegistryGui() {
        initComponents();
        logger = AlcatrazLogger.getLogger();
        btNickname.setVisible(false);
        this.setVisible(true);
        tbPlayer.getModel().setValueAt("left top", 0, 0);
        tbPlayer.getModel().setValueAt("right bottom",3,2);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bgReady = new javax.swing.ButtonGroup();
        lbHeadline = new javax.swing.JLabel();
        lbServerAddr = new javax.swing.JLabel();
        cbServerAddr = new javax.swing.JComboBox();
        txNickname = new javax.swing.JTextField();
        rbNotReady = new javax.swing.JRadioButton();
        rbReady = new javax.swing.JRadioButton();
        window4tbPlayer = new javax.swing.JScrollPane();
        tbPlayer = new javax.swing.JTable();
        btRegistry = new javax.swing.JButton();
        btStart = new javax.swing.JButton();
        btNickname = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lbHeadline.setFont(new java.awt.Font("Tahoma", 2, 18));
        lbHeadline.setText("Welcome to Alcatraz");

        lbServerAddr.setText("server address");

        cbServerAddr.setEditable(true);
        cbServerAddr.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "localhost" }));
        cbServerAddr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbServerAddrActionPerformed(evt);
            }
        });

        txNickname.setText("your nickname");

        bgReady.add(rbNotReady);
        rbNotReady.setSelected(true);
        rbNotReady.setText("not ready");
        rbNotReady.setEnabled(false);

        bgReady.add(rbReady);
        rbReady.setText("ready");
        rbReady.setEnabled(false);

        tbPlayer.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"", "", ""},
                {"", "", ""},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Player", "Color", "State"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tbPlayer.setEnabled(false);
        tbPlayer.setFocusable(false);
        window4tbPlayer.setViewportView(tbPlayer);

        btRegistry.setText("Registry");
        btRegistry.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRegistryActionPerformed(evt);
            }
        });

        btStart.setText("Start");
        btStart.setEnabled(false);

        btNickname.setText("send nickname");
        btNickname.setDefaultCapable(false);
        btNickname.setFocusPainted(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(93, 93, 93)
                        .addComponent(lbHeadline, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(window4tbPlayer, 0, 0, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lbServerAddr)
                            .addComponent(btNickname, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txNickname, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)
                            .addComponent(cbServerAddr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(rbNotReady)
                            .addComponent(rbReady)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btRegistry)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 233, Short.MAX_VALUE)
                        .addComponent(btStart)))
                .addGap(41, 41, 41))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbHeadline)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lbServerAddr)
                            .addComponent(cbServerAddr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btNickname)
                            .addComponent(txNickname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(rbReady))
                    .addComponent(rbNotReady))
                .addGap(28, 28, 28)
                .addComponent(window4tbPlayer, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btRegistry)
                    .addComponent(btStart))
                .addGap(33, 33, 33))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btRegistryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRegistryActionPerformed
        System.out.println("button registry pressed");
    }//GEN-LAST:event_btRegistryActionPerformed

    private void cbServerAddrActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbServerAddrActionPerformed
        System.out.println("action of combo box performed");
        int itemCount = cbServerAddr.getItemCount();
        int selectedIdx = cbServerAddr.getSelectedIndex();
        String selectedItem = (String)cbServerAddr.getSelectedItem();

        for(int i=0;i<cbServerAddr.getItemCount();i++) {
            logger.log(Level.INFO, "Item: ".concat((String)cbServerAddr.getItemAt(i)));
        }
        logger.log(Level.INFO, "selected Item index: ".concat(String.valueOf(selectedIdx)));
        logger.log(Level.INFO, "selected Item index old: ".concat(String.valueOf(cbServerAddrIdx)));
        logger.log(Level.INFO, "selected Item: ".concat(selectedItem));
        logger.log(Level.INFO, "nr of Item: ".concat(String.valueOf(itemCount)));


//            if("".equals(selectedItem) || itemCount >= MAX_SELECTED_SERVER_ADDR) {
//                cbServerAddr.removeItemAt(cbServerAddrIdx);
//            }
        if (selectedIdx<0) {
            boolean removable = itemCount > 1;
            boolean empty = "".equals(selectedItem);
            if( empty && removable || itemCount >= MAX_SELECTED_SERVER_ADDR) {
                cbServerAddr.removeItemAt(cbServerAddrIdx);
            }
            if(!empty) {
                if(itemCount < MAX_SELECTED_SERVER_ADDR) {
                    cbServerAddr.addItem(selectedItem);
                    cbServerAddrIdx = itemCount;
                } else {
                    cbServerAddr.insertItemAt(selectedItem, cbServerAddrIdx);
                }
            }
            cbServerAddr.setSelectedIndex(cbServerAddrIdx);
        } else {
            cbServerAddrIdx = selectedIdx;
        }
    }//GEN-LAST:event_cbServerAddrActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup bgReady;
    private javax.swing.JButton btNickname;
    private javax.swing.JButton btRegistry;
    private javax.swing.JButton btStart;
    private javax.swing.JComboBox cbServerAddr;
    private javax.swing.JLabel lbHeadline;
    private javax.swing.JLabel lbServerAddr;
    private javax.swing.JRadioButton rbNotReady;
    private javax.swing.JRadioButton rbReady;
    private javax.swing.JTable tbPlayer;
    private javax.swing.JTextField txNickname;
    private javax.swing.JScrollPane window4tbPlayer;
    // End of variables declaration//GEN-END:variables

}
